package ghostcat.display.bitmap
{
    import flash.display.*;
    import flash.events.*;
    import ghostcat.events.*;
    import ghostcat.util.*;

    public class BitmapMouseChecker extends Object
    {
        private var _enabled:Boolean = false;
        public var mouseDown:Boolean = false;
        public var bitmap:Bitmap;
        public var maskHitArea:Boolean = true;
        public var mouseOver:Boolean = false;
        private var _enabledTick:Boolean = false;

        public function BitmapMouseChecker(param1:Bitmap)
        {
            this.bitmap = param1;
            this.enabled = true;
            return;
        }// end function

        public function get enabled() : Boolean
        {
            return _enabled;
        }// end function

        private function mouseDownHandler(event:MouseEvent) : void
        {
            if (event.currentTarget != event.target)
            {
                return;
            }
            var _loc_2:* = isMouseOver();
            if (_loc_2)
            {
                mouseDown = true;
                bitmap.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN, true, false, bitmap.mouseX, bitmap.mouseY, null, event.ctrlKey, event.altKey, event.shiftKey, event.buttonDown));
            }
            return;
        }// end function

        public function set enabled(param1:Boolean) : void
        {
            if (_enabled == param1)
            {
                return;
            }
            _enabled = param1;
            if (param1)
            {
                bitmap.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
                bitmap.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
                bitmap.stage.addEventListener(MouseEvent.CLICK, clickHandler);
            }
            else
            {
                bitmap.stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
                bitmap.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
                bitmap.stage.removeEventListener(MouseEvent.CLICK, clickHandler);
            }
            enabledTick = param1;
            return;
        }// end function

        private function mouseUpHandler(event:MouseEvent) : void
        {
            if (event.currentTarget != event.target)
            {
                return;
            }
            mouseDown = false;
            var _loc_2:* = isMouseOver();
            if (_loc_2)
            {
                bitmap.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP, true, false, bitmap.mouseX, bitmap.mouseY, null, event.ctrlKey, event.altKey, event.shiftKey, event.buttonDown));
            }
            return;
        }// end function

        public function get enabledTick() : Boolean
        {
            return _enabledTick;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            if (event.currentTarget != event.target)
            {
                return;
            }
            var _loc_2:* = isMouseOver();
            if (_loc_2)
            {
                bitmap.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false, bitmap.mouseX, bitmap.mouseY, null, event.ctrlKey, event.altKey, event.shiftKey, event.buttonDown));
            }
            return;
        }// end function

        public function isMouseOver() : Boolean
        {
            if (!bitmap.bitmapData)
            {
                return false;
            }
            try
            {
                if (maskHitArea)
                {
                    return uint(bitmap.bitmapData.getPixel32(bitmap.mouseX, bitmap.mouseY) >> 24) > 0;
                }
                else
                {
                    return bitmap.bitmapData.rect.contains(bitmap.mouseX, bitmap.mouseY);
                }
            }
            catch (e:Error)
            {
            }
            return false;
        }// end function

        private function tickHandler(event:TickEvent) : void
        {
            var _loc_2:* = isMouseOver();
            if (mouseOver != _loc_2)
            {
                mouseOver = _loc_2;
                if (_loc_2)
                {
                    bitmap.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER, true, false, bitmap.mouseX, bitmap.mouseY));
                    bitmap.dispatchEvent(new MouseEvent(MouseEvent.ROLL_OVER, false, false, bitmap.mouseX, bitmap.mouseY));
                }
                else
                {
                    bitmap.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT, true, false, bitmap.mouseX, bitmap.mouseY));
                    bitmap.dispatchEvent(new MouseEvent(MouseEvent.ROLL_OUT, false, false, bitmap.mouseX, bitmap.mouseY));
                }
            }
            return;
        }// end function

        public function destory() : void
        {
            enabled = false;
            return;
        }// end function

        public function set enabledTick(param1:Boolean) : void
        {
            if (_enabledTick == param1)
            {
                return;
            }
            _enabledTick = param1;
            if (_enabled && _enabledTick)
            {
                Tick.instance.addEventListener(TickEvent.TICK, tickHandler);
            }
            else
            {
                Tick.instance.removeEventListener(TickEvent.TICK, tickHandler);
            }
            return;
        }// end function

    }
}
